package com.crm.service.after_sale.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.crm.dao.after_sale.AfterSaleServiceDao;
import com.crm.pojo.AftetSaleServer;
import com.crm.service.after_sale.AftetSaleServerService;
import com.crm.vo.Result;

@Service
@Transactional(rollbackFor = Exception.class)
public class AftetSaleServerImpl implements AftetSaleServerService{

	@Autowired
	private AfterSaleServiceDao afterSaleServiceDao;
	
	
	@Override
	public List<AftetSaleServer> findOneNotDel(Long serverId, int curpage, int pagesize) {
		PageRequest page = new PageRequest(curpage - 1, pagesize);
		return afterSaleServiceDao.findOneNotDel(serverId, page);
	}

	@Override
	public List<AftetSaleServer> findAllNotDelBypage(int curpage, int pagesize) {
		PageRequest page = new PageRequest(curpage - 1, pagesize);
		return afterSaleServiceDao.findAllNotDelBypage(page);
	}

	@Override
	public Long countAllNotDelBypage() {
		return afterSaleServiceDao.countAllNotDelBypage();
	}

	@Override
	public Result addAftetSaleServe(AftetSaleServer aftetSaleServer) {
		try {
			aftetSaleServer.setAftetSaleServerIsDel("否");
			afterSaleServiceDao.save(aftetSaleServer);
			return Result.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return new Result("对不起，新增售后服务失败！");
		}
	}

	@Override
	public Result delAftetSaleServe(Long serverId) {
		try {
			AftetSaleServer aftetSaleServer = afterSaleServiceDao.findOne(serverId);
			aftetSaleServer.setAftetSaleServerIsDel("是");
			afterSaleServiceDao.save(aftetSaleServer);
			return Result.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return new Result("对不起，删除售后服务失败！");
		}
	}

	@Override
	public Result editAftetSaleServe(AftetSaleServer aftetSaleServer) {
		try {
			aftetSaleServer.setAftetSaleServerIsDel("否");
			afterSaleServiceDao.save(aftetSaleServer);
			return Result.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return new Result("对不起，编辑售后服务失败！");
		}
	}

	@Override
	public List<AftetSaleServer> findByTheme(String theme, int curpage, int pagesize) {
		PageRequest page = new PageRequest(curpage - 1, pagesize);
		return afterSaleServiceDao.findByTheme("%"+theme+"%", page);
	}

	@Override
	public Long countByTheme(String theme) {
		return afterSaleServiceDao.countByTheme("%"+theme+"%");
	}

	
}
